﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表示流程流转上下文
    /// </summary>
    public class ProcessFlowContext
    {
        private ComponentSettings _compSetting;
        private Guid _userId = Guid.Empty;
        private User _user;
        private Guid _curActId = Guid.Empty;
        private Activity _curAct;
        private Guid _curActInsId;
        private ActivityInstance _curActIns;
        private Guid _procId = Guid.Empty;
        private Process _proc;
        private Guid _procInsId = Guid.Empty;
        private ProcessInstance _procIns;
        private Guid _flowSetpId = Guid.Empty;
        private FlowSetp _flowSetp;

        public Guid UserId
        {
            get
            { return _userId; }
            set
            {
                if (_userId != value)
                {
                    _userId = value;
                    _user = null;
                }
            }
        }
        public Guid CurrentActivityId
        {
            get { return _curActId; }
            set
            {
                if (_curActId != value)
                {
                    _curActId = value;
                    _curAct = null;
                }
            }
        }
        public Guid CurrentActivityInstanceId
        {
            get { return _curActInsId; }
            set
            {
                if (_curActInsId != value)
                {
                    _curActInsId = value;
                    _curActIns = null;
                }
            }
        }
        public Guid CurrentProcessId
        {
            get { return _procId; }
            set
            {
                if (_procId != value)
                {
                    _procId = value;
                    _proc = null;
                }
            }
        }
        public Guid CurrentProcessInstanceId
        {
            get { return _procInsId; }
            set
            {
                if (_procInsId != value)
                {
                    _procInsId = value;
                    _procIns = null;
                }
            }
        }
        public Guid CurrentFlowSetpId
        {
            get { return _flowSetpId; }
            set
            {
                if (_flowSetpId != value)
                {
                    _flowSetpId = value;
                    _flowSetp = null;
                }
            }
        }

        public User User
        {
            get
            {
                if (_userId != Guid.Empty && _user == null)
                {
                    _user = this._compSetting.UserProvider.Get((Guid)this._userId);
                }
                return this._user;
            }
            set
            {
                this._userId = value == null ? Guid.Empty : value.Identity;
                this._user = value;
            }
        }
        public Process CurrentProcess
        {
            get
            {
                if (_procId != Guid.Empty && _proc == null)
                {
                    _proc = this._compSetting.ProcessProvider.Get((Guid)this._procId);
                }
                return this._proc;
            }
            set
            {
                this._procId = value == null ? Guid.Empty : value.Identity;
                this._proc = value;
            }
        }
        public Activity CurrentActivity
        {
            get
            {
                if (_curActId != Guid.Empty && _curAct == null)
                {
                    _curAct = this.CurrentProcess.Activitys.FirstOrDefault(m => m.Identity == this._curActId);
                }
                return this._curAct;
            }
            set
            {
                this._curActId = value == null ? Guid.Empty : value.Identity;
                this._curAct = value;
            }
        }
        public ProcessInstance CurrentProcessInstance
        {
            get
            {
                if (_procInsId != Guid.Empty && _procIns == null)
                {
                    _procIns = this._compSetting.ProcessInstanceProvider.Get(this._procInsId);
                }
                return this._procIns;
            }
            set
            {
                this._procInsId = value == null ? Guid.Empty : value.Identity;
                this._procIns = value;
            }
        }
        public ActivityInstance CurrentActivityInstance
        {
            get
            {
                if (_curActInsId != Guid.Empty && _curActIns == null)
                {
                    _curActIns = this._compSetting.ActivityInstanceProvider.Get(this._curActInsId);
                }
                return this._curActIns;
            }
            set
            {
                this._curActInsId = value == null ? Guid.Empty : value.Identity;
                this._curActIns = value;
            }
        }
        public FlowSetp CurrentFlowSetp
        {
            get
            {
                if (_flowSetpId != Guid.Empty && _flowSetp == null)
                {
                    _flowSetp = this._compSetting.FlowSetpProvider.Get(this._flowSetpId);
                }
                return this._flowSetp;
            }
            set
            {
                this._flowSetpId = value == null ? Guid.Empty : value.Identity;
                this._flowSetp = value;
            }
        }

        public ProcessFlowContext(Guid userId, Guid procId, Guid procInsId, Guid actId, Guid actInsId, Guid flowSetpId, ComponentSettings compSetting)
        {
            this._userId = userId;
            this._procId = procId;
            this._procInsId = procInsId;
            this._curActId = actId;
            this._curActInsId = actInsId;
            this._flowSetpId = flowSetpId;
            this._compSetting = compSetting;
        }

        public ProcessFlowContext(ComponentSettings compSetting)
        {
            this._compSetting = compSetting;
        }
    }
}
